<template>

  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div><span>电商后台管理系统</span></div>
      <el-button type="primary" @click="logout" class="output">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->

      <el-aside :width="isCollapse ? '64px' : '200px'" style="transition: all 0.5s">
        <!-- 侧边栏菜单区域 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu background-color="grey" text-color="#fff" active-text-color="#409bff" unique-opened
          :collapse="isCollapse" :collapse-transition="false" :router="true" default-active>
          <!-- 一级菜单 -->
          <!-- 动态使index绑定值 -->
          <el-submenu :index="item.id +''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconfontsObj[item.id]"></i>
              <!-- 文本 -->
              <span class="wenben">{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path +''" v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>


          </el-submenu>

        </el-menu>
      </el-aside>
      <!-- 右侧内容主题区 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
  export default {
    data() {
      return {
        menulist: [],
        iconfontsObj: {
          '125': "iconfont icon-ziyuan57",
          '103': "iconfont icon-ziyuan80",
          '101': "iconfont icon-ziyuan83",
          '102': "iconfont icon-ziyuan84",
          '145': "iconfont icon-ziyuan74"
        },
        // 是否折叠
        isCollapse: false,
        // // 被激活的链接地址
        // activePath:''

      }
    },
    // 生命周期函数
    created() {
      this.getMenuList()
    },
    methods: {
      logout() {
        window.sessionStorage.clear();
        this.$router.push('/login')

      },
      // 获取所有的菜单
      async getMenuList() {
        const { data: res } = await this.$http.get('menus');
        console.log(res);
        if (res.meta.status != 200) return this.$message.error(res.meta.msg)
        this.menulist = res.data
      },
      // 点击按钮,展开或者关闭菜单
      toggleCollapse() {
        this.isCollapse = !this.isCollapse;

      },
      // // 保存链接的激活状态
      // saveNavState(activePath){
      //   window.sessionStorage.setItem('activePath',activePath)

      // }
    }

  }

</script>
<style lang='less' scoped>
  .home-container {
    height: 100%;
    overflow: hidden;
    
  }

  .el-header {
    display: flex;
    justify-content: space-between;
    background-color: #4bc2b886;
    padding-left: 0;
    align-items: center;
    color: black;
    font-size: 20px;
  }

  .el-aside {
    background-color: grey;

    .el-menu {
      border-right: none;
    }
  }

  .iconfont {
    margin-right: 10px;
    color: rgb(0, 225, 255);
  }

  .toggle-button {
    background-color: rgb(33, 94, 129);
    font-size: 10px;
    line-height: 24px;
    color: white;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }

  .wenben:hover {
    color: blue;

  }
 
</style>